package com.example.cloudpracticeplatform.mapper;

import com.example.cloudpracticeplatform.entity.Enrollment;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;

@Mapper
public interface EnrollmentMapper {
    // 查询是否存在对应的 studentId 和 courseId
    @Select("SELECT COUNT(*) FROM enrollments WHERE student_id = #{studentId} AND course_id = #{courseId}")
    int existsEnrollment(@Param("studentId") Integer studentId, @Param("courseId") Integer courseId);

    // 插入新记录
    @Insert("INSERT INTO enrollments (student_id, course_id, enrollment_date) VALUES (#{studentId}, #{courseId}, #{enrollmentDate})")
    int insertEnrollment(@Param("studentId") Integer studentId, @Param("courseId") Integer courseId, @Param("enrollmentDate") LocalDateTime enrollmentDate);

    // 更新 enrollment_date
    @Update("UPDATE enrollments SET enrollment_date = #{enrollmentDate} WHERE student_id = #{studentId} AND course_id = #{courseId}")
    int updateEnrollmentDate(@Param("studentId") Integer studentId, @Param("courseId") Integer courseId, @Param("enrollmentDate") LocalDateTime enrollmentDate);
    
    @Select("SELECT * FROM enrollments WHERE student_id = #{studentId} AND course_id = #{courseId}")
    Enrollment findByStudentIdAndCourseId(@Param("studentId") Integer studentId, @Param("courseId") Integer courseId);
}